using System.Linq;

namespace Core.Features
{
    using System;
    using Base;
    using Contracts.Enums;
    using Contracts.Interfaces;
    using Resources;

    
    public sealed class MediumLaserCycleTime<T> : FeatureBase<object>
    {
        public MediumLaserCycleTime(){}

        public MediumLaserCycleTime(T value) 
            : base(value){ }
        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Cycle time"; }
        }
        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup{ get { return FeatureGroupsEnum.Specific; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.MediumLaserCycleTime; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_sec; } }

        public override void SetModifiedValue(IAgent agent, IRobot robot)
        {
            if (agent == null || robot == null) return;

            // Bonuses from extensions
            var extensionBonus = (1 + Math.Abs(agent.Extensions.SumModifiersValues(FeatureTypesEnum.Module, this.FeatureType)));

            // Bonuses from bot
            var robotBonus = (1 + Math.Abs(robot.Modifiers.SumModifiersValues(agent.Extensions, FeatureTypesEnum.Module, this.FeatureType)));

            var temp = (double)Value / extensionBonus / robotBonus;

            // Bonus from Tunning modules
            temp = robot.Modules.Aggregate(
                temp,
                (current, mod) => current - (current * Math.Abs(mod.Value)),
                FeatureTypesEnum.Module,
                this.FeatureType,
                ModuleGroupsEnum.WeaponTunings);

            // nexus Bonus
            double nexusBonus = robot.Modules.SumModifiersValues(FeatureTypesEnum.Module, this.FeatureType, ModuleGroupsEnum.NexusModuleAssault);
            double nexusSkillBonus = agent.Extensions.SumModifiersValues(FeatureTypesEnum.Robot, this.FeatureType);

            double nexusTotalBonus = 0;

            if (nexusBonus != 0)
            {
                nexusTotalBonus = (1 - Math.Abs(nexusBonus)) / (1 + Math.Abs(nexusSkillBonus));
                nexusTotalBonus = 1 - nexusTotalBonus;
            }
            temp = temp * (1 - nexusTotalBonus);

            ModifiedValue = IsRounded
                ? Math.Round(temp, 0, MidpointRounding.AwayFromZero)
                : Math.Round(temp, 2);
        }
    }
}